import request from '@/config/request.js'
import { getAccessToken, getSerialNo, getTenantId } from '@/utils/auth'
import store from '@/store'
import QuestionParserService from '@/assets/js/xkw-xop-js-qbmsdk-1.0.0/xkw-xop-qbmsdk'

const questionParserService = new QuestionParserService()

// 获取字典数据
export const listSimpleDictDataApi = () => {
  return request.get('/admin-api/system/dict-data/list-all-simple')
}

// 上传文件
export const uploadFile = async (data) => {
  return request.post('/app-api/infra/file/app/upload', data, {
    header: {
      'content-type': 'multipart/form-data',
      Authorization: await getAccessToken(),
      serialNo: await getSerialNo(),
      'tenant-id': await getTenantId()
    }
  })
}

// 获取所有科目
export const getAllSubjectApi = ({
  formatter = (subjects) => {
    return subjects
  }
}) => {
  return new Promise(async (resolve) => {
    const subjects = await store.dispatch('getDictByType', 'subject')
    resolve({
      code: 0,
      data: formatter(subjects),
      msg: ''
    })
  })
}

// 获取自身所开通的模块科目
export const getSelfModuleSubjectApi = () => {
  return request.get('/app-api/system/app/app-module-subject/getSelfModuleSubject')
}

// 创建模块使用时长统计
export const createModuleUseTimeApi = (data) => {
  return request.post('/app-api/exam/app/module-use-time/create', data)
}

// 批量创建使用时长统计
export const createBatchModuleUseTimeApi = (data) => {
  return request.post('/app-api/exam/app/module-use-time/createBatch', data)
}

// 创建异常分析
export const creatEexceptionAnalysisApi = (data) => {
  return request.post('/admin-api/system/exception-analysis/create', data)
}

// 题目解析
export const questionSplitApi = (data) => {
  return new Promise(async (resolve) => {
    const res = data.map((question) => {
      const { questionId, stem, answer, explanation } = question

      return {
        questionId,
        stem: stem ? JSON.stringify(questionParserService.splitStem(stem)) : 'null',
        answer: answer ? JSON.stringify(questionParserService.splitAnswer(answer)) : 'null',
        explanation: explanation
          ? JSON.stringify(questionParserService.splitExplanation(explanation))
          : 'null'
      }
    })

    resolve({
      code: 0,
      data: res,
      msg: ''
    })
  })
}
